<?php

/**
 * sfPaymentPayPal Class
 *
 * This provides support for PayPal to sfPaymentPlugin. It has 
 * been inspired from Md Emran Hasan work (http://www.phpfour.com).
 *
 * @package   sfPaymentPayPal
 * @category  Library
 * @author    Md Emran Hasan <phpfour@gmail.com>
 * @author    Johnny Lattouf <johnny.lattouf@letscod.com>
 * @author    Antoine Leclercq <antoine.leclercq@letscod.com>
 * @link      http://www.phpfour.com
 * @link      http://wiki.github.com/letscod/sfPaymentPlugin
 */

class sfPaymentDenarii extends sfPaymentGatewayInterface {
	
	public function __construct() {
		parent::__construct();
		
		// translation table
		$this->addFieldTranslation('TransactionType',          'Transaction Type');
		$this->addFieldTranslation('MerchantId',        'Merchant Id');
                $this->addFieldTranslation('MerchantPasscode',          'Merchant Passcode');
                $this->addFieldTranslation('Amount',     'Amount');
                $this->addFieldTranslation('CreditCardType',    'CreditCardType');
                $this->addFieldTranslation('PAN',          'PAN');
                $this->addFieldTranslation('PANExpDate',    'PAN Exp Date');
                $this->addFieldTranslation('Email',          'E-mail');
                $this->addFieldTranslation('CVV',              'CVV2');
                $this->addFieldTranslation('FirstName',             'FirstName');
                $this->addFieldTranslation('LastName',             'LastName');
                $this->addFieldTranslation('AVSAddress',             'AVS Address');
                $this->addFieldTranslation('City',             'City');
                $this->addFieldTranslation('State',             'State');
                $this->addFieldTranslation('Country',             'Country');
                $this->addFieldTranslation('Phone',             'Phone');
                $this->addFieldTranslation('AVSZipCode',             'AVS Zip Code');
                $this->addFieldTranslation('PreviousAuthorizationId',             'Previous Authorization Id');
                $this->addFieldTranslation('ReferenceTransactionId',             'Reference Transaction Id');

    // default values of the class
		$this->gatewayUrl = "http://www.denariicards.com/ipgwebservice/IPGSecure.aspx";
		$this->ipnLogFile = 'denarii.log';
                $this->setTransactionType(2);
                $this->setMerchantId('30004');
                $this->setMerchantPasscode('cash');
                $this->setAmount(1);
                $this->setCreditCardType('Visa');
                $this->setPAN('4547010707341237');
                $this->setPANExpDate('1111');
                $this->setEmail('abcde@aol.com');
                $this->setCVV('12354');
                $this->setFirstName('John');
                $this->setLastName('Smith');
                $this->setAVSAddress('123 Main Street');
                $this->setCity('DORAL');
                $this->setState('FL');
                $this->setCountry('USA');
                $this->setPhone(305-000-0001);
                $this->setAVSZipCode(33172);
                $this->setPreviousAuthorizationId(0);
                $this->setReferenceTransactionId(0);



		// populate $fields array with a few default
		//$this->setRm('2');           // return method = POST
		//$this->setCmd('_xclick');
		
		// set from config values
		//$this->setReturn(url_for(sfConfig::get('app_sf_payment_paypal_plugin_return','pagos/success'),true));
		//$this->setCancelReturn(url_for(sfConfig::get('app_sf_payment_paypal_plugin_cancel_return','pagos/failure'),true));
		//$this->setNotify(url_for(sfConfig::get('app_sf_payment_paypal_plugin_notify','pagos/ipn'),true));

		/*if(sfConfig::get('app_sf_payment_paypal_plugin_business'))
		  $this->setVendor(sfConfig::get('app_sf_payment_paypal_plugin_business'));
		else
		  throw new sfException('No business paypal acccount referenced in app.yml.<br />Please check the README file.');
                *
                 */
	}
	
	/**
	 * Enables test mode
	 *
	 * @param none
	 * @return none
	 */
	public function enableTestMode()
        {
                $this->testMode = true;
                $this->gatewayUrl = 'https://www.sandbox.paypal.com/cgi-bin/webscr';

                $test = sfConfig::get('app_sf_payment_paypal_plugin_test');

                if(isset($test['business']))
                  $this->setVendor($test['business']);
                else
                  throw new sfException('No test business paypal acccount referenced in app.yml.<br />Please check the README file.');
         }
    
 	/**
	 * Validate the IPN notification
	 *
	 * @param none
	 * @return boolean
	 */
	public function validateIpn($parameters = array())
	{
		// retrieve the parameters
		$this->ipnData = $parameters;
		//$parameters["cmd"] = "_notify-validate";
		$browser = new sfWebBrowser(array("Content-type: application/x-www-form-urlencoded\r\n","Connection: close\r\n\r\n"));
		$browser->post($this->gatewayUrl, $parameters);
		
		$this->ipnResponse= $browser->getResponseText();
		
		if($browser->getResponseText() === "VERIFIED")
    {
      // Valid IPN transaction.
      $this->logResults(true);
      return true;
    }
    else
    {
    	// Invalid IPN transaction.  Check the log for details.
      $this->lastError = "IPN Validation Failed . ".$this->gatewayUrl;
      $this->logResults(false);
      return false;
    }
	}
	
	/**
	 * Check if the payment status is completed after ipn validation 
	 *
	 * @return boolean
	 */
	public function isCompleted() {
		if($this->ipnData['payment_status'] == 'Completed')
		  return true;
		else
		  return false;
	}
}